module CustomHelpers
  
  def inside_layout(layout, &block)
    @template.instance_variable_set("@content_for_layout", capture(&block));
    layout = layout.include?("/") ? layout : "layouts/#{layout}";
    buffer = eval("_erbout", block.binding);
    buffer.concat(@template.render_file(layout, true));
  end
  
  def label_for(object_name, attr, content=nil)
    object = instance_variable_get("@"+object_name.to_s);
    klass = (object) ? object.class : nil;
    unless klass
      klass = object_name.classify.constantize rescue nil;
    end
    if klass
      content = klass::AttributesNames[attr.to_sym] || content || attr.to_s;
      id = "#{object_name}_#{attr}";
    else; content, id = attr, object_name; end
    content_tag("label", content, {:for => id});
  end
  
  def content_for_default(name, *args, &block)
    unless instance_variables.include?("@content_for_"+name.to_s)
      content_for(name, *args, &block);
    end
  end
 
  def render_subpage(opts={}, &filter)
    opts = params.merge(:text => "<div id=\"subpage\">\n%s\n</div>").merge(opts).symbolize_keys;
    path = "#{opts[:controller]}/#{opts[:action]}/#{opts[:subpage]}";
    subpage_opts = opts[opts[:subpage].to_sym] || {};
    subpage_opts = {
      :filter => ((block_given?) ? filter : lambda{|t| t}), 
      :template => path,
      :component => nil
    }.merge(subpage_opts.symbolize_keys);
    body = (subpage_opts[:component]) ? render_component(*subpage_opts[:component]) : render(subpage_opts[:template]);
    subpage_opts[:filter].call(opts[:text] % body);
  end

  def item_link(name, url, opts={})
    # opts[:status] = (:disabled || :inactive) || (:enable || :condition) || :active
    opts = {:inactive_class => "inactive", :active_class => "active", :status => :condition, :params_not_used => []}.merge(opts);
    not_params = [opts[:params_not_used]].flatten;
    url = url.remove(*not_params) if url.kind_of?(Hash);
    status = opts[:status];
    actual_uri = request.request_uri; #url_for(@request.parameters.remove(*not_params));
    klass = " class=\"%s\"";
    klass = (actual_uri == url_for(url) || status == :active) ? klass % opts[:active_class] : ([:inactive, :disabled].include?(status)) ? klass % opts[:inactive_class] : "";
    link = ([:inactive, :disabled].include?(status)) ? content_tag("a", name) : link_to(name, url);
    "<li#{klass}>#{link}</li>";
  end
    
end

ActionView::Base.send(:include, CustomHelpers);

module ControllerMethods
  
  def errors(obj, opts={})
    opts = {:class => "", :message => ""}.merge(opts);
    klass = " class=#{opts[:class]}" if opts[:class] != "";
    msg = opts[:message] + "<br />" if opts[:message] != "";
    tag = "#{msg}\n<ul#{klass}>\n";
    attrs = obj.class::AttributesNames || {};
    attrs = Hash.new{|hash, key| hash[key] = key.to_s;}.merge(attrs);
    obj.errors.each do |attr, msg|
      tag << "<li>#{attrs[attr.to_sym]} #{msg}</li>\n";
    end
    tag + "</ul>";
  end

  def subpage(subpage, param=:subpage)
    redirect_to(param => subpage.to_s) unless params[param];
  end

end

ActionController::Base.send(:include, ControllerMethods)